const CityzonesGrids = require("./zonesGridsInSavageMap");
const ItemPickupData = require("./SavageMap_ItemPickup_InZones_Docs.json");
// const mongoConnected = require("../lib/mongoose_connect");
const mongoModels = require("../lib/mongoose_schemas");
const fs = require("fs-extra");
const lodash = require("lodash");
const path = require("path");

// debugger
const ByZonesStatics = {};
for (let zone of CityzonesGrids) {
  let name = zone['name']
  let _name = name.toLowerCase();
  while (_name.includes(" ")) {
    _name = _name.replace(" ", "")
  }
  ByZonesStatics[_name] = {
    grids_count: zone['grids_count'],
    name: zone['name'],
    item_count: 0,
    "Ammunition": 0,
    "Attachment": 0,
    "Equipment": 0,
    "Event": 0,
    "Use": 0,
    "Weapon": 0
  }
}
// let usesSubcategory = new Set;
// let uses = ItemPickupData.filter(e => e.item.category == "Weapon");
// uses.forEach(e => {
//   usesSubcategory.add(e.item.subCategory)
// })
// console.log(Array.from(usesSubcategory));
// debugger
for (let ev of ItemPickupData) {
  for (let zonename of ev.character.zone) {
    ByZonesStatics[zonename].item_count += 1;
    // if (ev.item.category == "Event") {
    //   debugger
    // }
    ByZonesStatics[zonename][ev.item.category] += 1;
  }
}
let results = {};
for (let x of Object.values(ByZonesStatics)) {
  // debugger
  // console.log(x.name, "|", x.item_count, "|", x.item_count / x.grids_count);
  results[x.name] = {
    items: 0,
    Ammunition: 0,
    Attachment: 0,
    Equipment: 0,
    Event: 0,
    Use: 0,
    Weapon: 0
  }
}
// debugger
for (let x of Object.values(ByZonesStatics)) {
  // debugger
  // console.log(x.name, "|", x.item_count, "|", x.item_count / x.grids_count);
  results[x.name]['items'] = x.item_count / x.grids_count
}
console.log("-----")
for (let x of Object.values(ByZonesStatics)) {
  // console.log(x.name, "|", x.Ammunition, "|", x.Ammunition / x.grids_count);
  results[x.name]['Ammunition'] = x.Ammunition / x.grids_count;
}
console.log("-----")
for (let x of Object.values(ByZonesStatics)) {
  console.log(x.name, "|", x.Attachment, "|", x.Attachment / x.grids_count);
  results[x.name]['Attachment'] = x.Attachment / x.grids_count;
}
console.log("-----")
for (let x of Object.values(ByZonesStatics)) {
  console.log(x.name, "|", x.Equipment, "|", x.Equipment / x.grids_count);
  results[x.name]['Equipment'] = x.Equipment / x.grids_count
}
console.log("-----")
for (let x of Object.values(ByZonesStatics)) {
  console.log(x.name, "|", x.Event, "|", x.Event / x.grids_count);
  results[x.name]['Event'] = x.Event / x.grids_count
}
console.log("-----")
for (let x of Object.values(ByZonesStatics)) {
  console.log(x.name, "|", x.Use, "|", x.Use / x.grids_count);
  results[x.name]['Use'] = x.Use / x.grids_count
}
console.log("-----")
for (let x of Object.values(ByZonesStatics)) {
  console.log(x.name, "|", x.Weapon, "|", x.Weapon / x.grids_count);
  results[x.name]['Weapon'] = x.Weapon / x.grids_count
}
fs.writeFile(path.join(__dirname, "./SavageMap_ItemPickup_InZones0713S.json"),JSON.stringify(results),()=>{
  process.exit()
})

// let filtered = ItemPickupData.filter(e => {
//   return e.character.zone.length
// });
// fs.writeFile(path.join(__dirname, "./SavageMap_ItemPickup_InZones_Docs.json"), JSON.stringify(filtered), () => {
//   process.exit()
// })
debugger

// mongoConnected.then(async __ => {
//   let matches = await mongoModels.Match.find({
//     map_name: "Savage_Main",
//     team_size: 4
//   });
//   console.log("这里有", matches.length, "场次");
//   debugger
//   let match_ids = matches.map(e => e.match_id);
//   let item_events_cursor = await mongoModels.Event.find({
//     _T: "LogItemPickup",
//     match_id: {
//       $in: match_ids
//     }
//   }).cursor();
//   let itemPickupDocs = []
//   item_events_cursor.on("data",doc=>{

//     itemPickupDocs.push(doc._doc);
//   });
//   item_events_cursor.on("close",()=>{
//     debugger
//     fs.writeFile(path.join(__dirname,"./SavageMap_ItemPickup_Docs.json"),JSON.stringify(itemPickupDocs),()=>{
//       debugger
//     })
//   })
//   debugger
// })